﻿//===============================================================================
// Microsoft Innovation Centre - Western Europe
// Copyright (c) 2008 Microsoft Corporation. All rights reserved.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows.Controls;
using System.Windows.Data;
using Microsoft.WVB.Framework.DataServicesEntities;
using Microsoft.WVB.Framework.Interfaces;
using Microsoft.WVB.Silverlight.Infrastructure;

namespace Microsoft.WVB.UI
{
    public class EPGModel : BaseController
    {
        private IBroadcasterProvider IBroadcaster = null;

        public int SelectedDay
        {
            get
            {
                return _selectedDay;
            }
            private set
            {
                _selectedDay = value;
                OnPropertyChanged(this.GetPropertySymbol(o => o.SelectedDay));
            }
        }
        private int _selectedDay;

        public int SelectedMonth
        {
            get
            {
                return _selectedMonth;
            }
            private set
            {
                _selectedMonth = value;
                OnPropertyChanged(this.GetPropertySymbol(o => o.SelectedMonth));
            }
        }
        private int _selectedMonth;

        public Channel SelectedChannel
        {
            get
            {
                return _selectedChannel;
            }
            private set
            {
                _selectedChannel = value;
                OnPropertyChanged(this.GetPropertySymbol(o => o.SelectedChannel));
            }
        }
        private Channel _selectedChannel;

        public int SelectedTime
        {
            get
            {
                return _selectedTime;
            }
            private set
            {
                _selectedTime = value;
                OnPropertyChanged(this.GetPropertySymbol(o => o.SelectedTime));
            }
        }
        private int _selectedTime;

        public List<int> CurrentDaysInMonth
        {
            get
            {
                return _currentDaysInMonth;
            }
            private set
            {
                _currentDaysInMonth = value;
                OnPropertyChanged(this.GetPropertySymbol(o => o.CurrentDaysInMonth));
            }
        }
        private List<int> _currentDaysInMonth;

        public List<int> Months
        {
            get
            {
                GregorianCalendar gregorianCalendar = new GregorianCalendar(GregorianCalendarTypes.USEnglish);
                int capacity = gregorianCalendar.GetMonthsInYear(DateTime.Now.Year);

                List<int> result = new List<int>();

                for (int i = 0; i < capacity; i++)
                {
                    result.Add(i + 1);
                }
                return result;
            }
        }

        public List<Channel> Channels { get; set; }

        public EPGModel()
            : base()
        {
            initializeValues();
        }

        private void initializeValues()
        {
            IBroadcaster = IoC.GetCreate<IBroadcasterProvider>();

            this.CurrentDaysInMonth = getDaysInMonth(DateTime.Now.Month);            
            this.SelectedDay = DateTime.Now.Day - 1;
            this.SelectedMonth = DateTime.Now.Month - 1;
            
            // Create Test data

            Channels = new List<Channel>();
            Channels.Add(IBroadcaster.CreateRandomChannelSchedule("TVE 1", "Media/Channels/TVE1.png"));
            Channels.Add(IBroadcaster.CreateRandomChannelSchedule("TVE 2", "Media/Channels/TVE2.png"));
            Channels.Add(IBroadcaster.CreateRandomChannelSchedule("Net TV", "Media/Channels/NetTV.png"));
            Channels.Add(IBroadcaster.CreateRandomChannelSchedule("Cautro", "Media/Channels/Cautro.png"));
            Channels.Add(IBroadcaster.CreateRandomChannelSchedule("VEO", "Media/Channels/Veo.png"));
        }

        /// <summary>
        /// Generates the time-of-day entries along the top of the timeline.
        /// </summary>
        public void BuildTimelineHeader(Panel layout)
        {
            TextBlock tb = new TextBlock();
            tb.Text = ":00";
            Canvas.SetLeft(tb, 0);
            layout.Children.Add(tb);

            for (int t = 1; t <= 9; t += 1)
            {
                TextBlock time = new TextBlock();
                time.Text = String.Format("{0}:00", t);
                Canvas.SetLeft(time, (t * 120) - 10);
                layout.Children.Add(time);
            }

            for (int t = 10; t <= 24; t += 1)
            {
                TextBlock time = new TextBlock();
                time.Text = String.Format("{0}:00", t);
                Canvas.SetLeft(time, (t * 120) - 17);
                layout.Children.Add(time);
            }
        }

        private List<int> getDaysInMonth(int month)
        {
            GregorianCalendar gregorianCalendar = new GregorianCalendar(GregorianCalendarTypes.USEnglish);
            int numberOfDays = gregorianCalendar.GetDaysInMonth(DateTime.Now.Year, month);

            List<int> days = new List<int>(numberOfDays);

            for (int i = 0; i < numberOfDays; i++)
            {
                days.Add(i + 1);
            }

            return days;
        }

        public void SetSelectedMonth(int monthValue)
        {
            if (this.SelectedMonth != monthValue)
            {
                this._selectedMonth = monthValue;
                this.CurrentDaysInMonth = getDaysInMonth(monthValue + 1);
                this.SelectedDay = 0;
            }
        }

        public void SetSelectedDay(int dayValue)
        {
            this._selectedDay = dayValue;
        }

        public void SetSelectedChannel(Channel channelValue)
        {
            this._selectedChannel = channelValue;
        }

        public void NextDay()
        {
            DateTime dt = new DateTime(DateTime.Now.Year, SelectedMonth + 1, SelectedDay + 1);
            DateTime newDt = dt.AddDays(1);
            UpdateModel(newDt);
        }

        public void PreviousDay()
        {
            DateTime dt = new DateTime(DateTime.Now.Year, SelectedMonth + 1, SelectedDay + 1);
            DateTime newDt = dt.AddDays(-1);
            UpdateModel(newDt);
        }

        private void UpdateModel(DateTime date)
        {
            if (date.Month != (this.SelectedMonth + 1))
            {
                this.CurrentDaysInMonth = getDaysInMonth(date.Month);
                this.SelectedMonth = date.Month - 1;
            }

            this.SelectedDay = date.Day - 1;            
        }        
    }

    public class IntToMonthNameConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value.GetType().Equals(typeof(List<int>)))
            {
                List<int> input = (List<int>)value;
                DateTimeFormatInfo dtInfo = new DateTimeFormatInfo();
                List<string> result = new List<string>(input.Count);

                for (int i = 0; i < input.Count; i++)
                {
                    result.Add(dtInfo.GetMonthName(input[i]));
                }

                return result;
            }
            else
                return new String[12];
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

}
